import axios from '../../http/http'
// import axios from 'axios'
import * as api from '../../http/api'


const state = {
    tableAData: [],
    canZhuoData: {
        canZhuoData: [],
        type: "",
        canState: '',
    },
    pager: {
        totalSize: 0

    }
}

const actions = {
    //页面加载获取数据
    getcanZhuoData({commit}, number) {
        return axios({
            url: api.canZhuo,
            method: 'post',
            data: {
                currentPage: number,
                pageSize: 4
            }

        }).then(res => {
            // console.log(res.data) 这个里面有3个东西  code data token
            // 目的是讲data储存在vuex里面
            for(let i=0;i<res.data.info.length;i++){
                if(res.data.info[i].state===0){
                    res.data.info[i].state='占用'
                }else if(res.data.info[i].state===1){
                    res.data.info[i].state='可用'
                }
            }
            // for(let i=0;i<res.data.info.length;i++){
            //     if(res.data.info[i].type==='方桌'){
            //         res.data.info[i].type='大厅'
            //     }else if(res.data.info[i].type==='大圆桌'){
            //         res.data.info[i].type='包间'
            //     }
            // }

            commit('zengData', res.data)
            this.pager = res.data.pager
            // console.log(res.data)
            // console.log(res.data.pager)
            return res.data
        })


    },
    //查询渲染相应数据
    selectCanZhuo({commit}, number) {
        axios({
            url: api.selectCanZhuo,
            method: 'post',
            data: {
                number: number,

            }
        }).then(res => {
            // console.log(res.data.data)
            for(let i=0;i<res.data.data.length;i++){
                if(res.data.data[i].state===0){
                    res.data.data[i].state='占用'
                }else if(res.data.data[i].state===1){
                    res.data.data[i].state='可用'
                }
            }
            // for(let i=0;i<res.data.data.length;i++){
            //     if(res.data.data[i].type==='方桌'){
            //         res.data.data[i].type='大厅'
            //     }else if(res.data.data[i].type==='大圆桌'){
            //         res.data.data[i].type='包间'
            //     }
            // }
            commit('addChaData', res.data.data)
        })


    }


}
const mutations = {
    zengData(state, obj) {
        state.canZhuoData.canZhuoData = obj.info
        state.pager = obj.pager
    },
    addChaData(state, obj) {
        state.canZhuoData.canZhuoData = obj
        state.pager.totalSize = obj.length
    },

    removeData(state) {
        state.canZhuoData.canZhuoData = []
        state.canZhuoData.type = ''
        state.canZhuoData.canState = ''
    }
}
const getters = {
    canZhuoType(state) {
        return state.canZhuoData.type
    },
    canZhuoState(state) {
        if (state.canZhuoData.canState == 0) {
            return '可用'
        } else if (state.canZhuoData.canState == 1) {
            return '占用'
        }
    }


}
export default {
    state,
    actions,
    mutations,
    getters
}